package ca.ericslandry.server.handler;

import javax.servlet.http.HttpSession;

import net.customware.gwt.dispatch.server.ExecutionContext;
import net.customware.gwt.dispatch.shared.ActionException;

import org.apache.commons.logging.Log;

import ca.ericslandry.server.dao.IssueDAO;
import ca.ericslandry.shared.domain.Issue;
import ca.ericslandry.shared.domain.User;
import ca.ericslandry.shared.rpc.GenericResult;
import ca.ericslandry.shared.rpc.SaveIssue;

import com.google.inject.Inject;
import com.google.inject.Provider;

public class SaveIssueHandler extends BaseSessionHandler<SaveIssue, GenericResult> {
	@Inject
	public SaveIssueHandler(Log logger, Provider<HttpSession> sessionProvider) {
		super(logger, sessionProvider);
	}

	@Override
	public GenericResult executeInternal(SaveIssue action, ExecutionContext context) throws ActionException {
		User user = getUser();
		Issue issue = action.getIssue();
		if (issue.getCreator() == null) {
			issue.setCreator(user);
		}
		IssueDAO dao = new IssueDAO();
		dao.put(issue);
		return new GenericResult("Saved issue id " + issue.getId(), true);
	}

	@Override
	public Class<SaveIssue> getActionType() {
		return SaveIssue.class;
	}
}
